home *** CD-ROM | disk | FTP | other *** search
- /**
- QTEST.C - This program is a sample to test the Queue functions.
- It is a very simple program used to demonstrate the use of queues
- (circular) in communicating with hardware. The basic Idea of this
- Program is that the user types anything at the keyboard and it is
- echoed back to them, pressing ESC ends the program. If they press
- CNTRL-A, then a string is inserted into the Queue, called TextBuffer.
- When the function MyGetCh() is called, if the user has not pressed
- a key, the Service() function is called which checks the Queue to
- see if any data is available, if so one character is removed and
- printed to the screen or printer (based on the value of ReallyPrint).
- The variable SLOWFACTOR can be incremented or decremented to slow
- down or speed up the printing process.
- To test the program, type in some text, and then press CNTRL-A, the
- QuePrint function inserts text into the buffer, and while the program
- is waiting for the next keystroke, it is also printing information
- from the buffer. Press CNTRL-A, and then several other keys quickly
- after to see you typed-in text mixed with the 'Queued' text.
-
- To Compile: TCC or QCL QTEST.C Q.OBJ (See Q.C for info to make Q.OBJ)
- Mario Giannini
- **/
- #include <stdio.h>
- #include "q.h"
-
- int ReallyPrint=0; /* Change to one and Que buffer goes to printer */
- int SLOWFACTOR=200; /* Slows down Que output to be visible (on a '386 anyway) */
-
- main()
- {
- Que *TextBuffer;
- int i;
-
- TextBuffer=CreateQ(2048);
- Service(TextBuffer);
-
- printf("Type in any text, and Ctrl-A To place text into the Que\n");
-
- while( (i=MyGetCh())!=27)
- {
- if(i==1)
- QuePrint(TextBuffer, "\nThis is the sample text to insert into the Que\n");
- else
- printf("%c", i);
- }
- DestroyQ(TextBuffer);
- }
-
-
- MyGetCh() /* getch replacement that calls Service() while idle */
- {
- while(!kbhit())
- Service(NULL);
- return(getch());
- }
-
- /* The Service func, prints a character from Que to printer or screen */
- Service(Que *Q2Register)
- {
- static Que *MyLocalQue=NULL;
- static int DoNothing=0;
-
- if(Q2Register!=NULL)
- MyLocalQue=Q2Register;
- if(MyLocalQue!=NULL && !Empty(MyLocalQue))
- {
- if(SLOWFACTOR && (DoNothing++%SLOWFACTOR))
- return; /* If func returns 9 out of 10 times and does nothing, result are more noticeable */
- if(ReallyPrint)
- fprintf(stdprn, "%c", GetFromQ(MyLocalQue));
- else
- printf("%c", GetFromQ(MyLocalQue));
- }
- }
-
- QuePrint(Que *Q2Print2, char *Str2Add) /* Copies a string into the Que */
- {
- while(*Str2Add)
- if(InsertIntoQ(Q2Print2, *Str2Add++)!=ALLOK)
- break;
- }